package com.muki.succ.uri;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

import com.muki.jms.JmsConsumer;

/**
 * uri接收者，从消息端接受url信息并发送到uri缓冲池
 * @author ljh_2017
 *
 */
public class MukiUriAcceptor implements UriCollector{

	private final Logger log = Logger.getLogger(MukiUriAcceptor.class);
	private JmsConsumer consumer;
	
	public MukiUriAcceptor() {
		consumer = new JmsConsumer();
	}
	
	@Override
	public void setListenerEntering(final String topic) {
		try {
			//绑定消息口topic
			consumer.bindQueue(topic);
			//设置监听queue
			consumer.getConsssumer().setMessageListener(new MessageListener(){

				@Override
				public void onMessage(Message message) {
					if(message instanceof TextMessage) {
						TextMessage text = (TextMessage) message;
						try {
							//将收集到的消息，发送到uri队列管理池
							log.info("推送消息到"+topic+"队列中");
							UriQueueManagerPool.getInstance().putUrl(topic, text.getText());
							message.acknowledge();
						} catch (JMSException e) {
							e.printStackTrace();
						}
					}
				}
			});
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
